<?php
/**
 * Created by PhpStorm.
 * User: papa
 * Date: 18/5/21
 * Time: 14:40
 */

namespace app\api\controller\v2;


use app\api\model\User;
use app\api\model\UserLevel;
use app\api\service\OrderService;
use app\api\service\Pay;
use app\api\service\TokenService;
use app\api\validate\Deposit;
use app\api\validate\FacePay;
use app\api\validate\OrderPlace;

class PayController extends BaseController
{
    //前置操作,权限控制
    protected $beforeActionList = [
        'checkLogin' => ['only' => 'FacePay','refund','FacePayOrder','orderPay','placeOrder','snPay','handlePrice','deposit'],
    ];

    //小程序提交生成订单的信息
    public function handlePrice($order_type)
    {
        $data = $this->request->post();
        //接收生成订单的数据并验证
        (new OrderPlace())->goCheck($data);
        $uid = TokenService::getCurrentUid();
        $user = User::get($uid);
        return OrderService::handlePrice($data,$user);
    }

    //商品下单
    public function orderPay()
    {
        (new OrderPlace())->goCheck();
        $data = input('post.');
        $uid = TokenService::getCurrentUid();
        $user = User::get($uid);
        return OrderService::foodOrder($data,$user);
    }

    //支付前初始化，获取后台开启优惠劵和积分信息，以及开启后获取对应优惠劵和积分信息
    public function facePay()
    {
        $uid = TokenService::getCurrentUid();
        $user = User::get($uid);
        //需要返回数据：可用优惠劵，积分数量
        return OrderService::doPaySet($uid);
    }

    //未付款订单，重新发起支付
    public function snPay()
    {
        $data = $this->request->param();
        return (new Pay())->snPay($data['sn'],$data['type']);
    }

    //当面付的支付
    public function facePayOrder()
    {
        (new FacePay())->goCheck();
        $data = input('post.');
        return (new Pay())->FacePay($data);
    }

    //支付结果异步通知
    public function payNotify()
    {
        (new Pay())->payNotify();
    }

    //退款接口
    public function refund($id)
    {
        $pay = new Pay();
        return $pay->handleRefund($id);
    }

    //用户充值
    public function deposit()
    {

        (new Deposit())->goCheck();
        $pay = new Pay();
        $price = input('post.price');
        return $pay->deposit($price);
    }

    //充值后的回调
    public function depositNotify()
    {
        $pay = new Pay();
        return $pay->depositNotify();
    }

    //测试数据
    public function test()
    {

    }
}